# -*- coding: utf-8 -*-
#服务器验证中间件

from lib.pihsdk import check_sign
from flask import request
import json
import redis
from lib.bfredis.bfredis import bfredis
from functools import wraps
cur_bf = None

#验签中间件
'''
def check_sign(*out_args, **out_kwargs):
    def make_dec(func):
        def dec_func(*args, **kwargs):
            print('check_sign')
            tmp_key = out_args[0]
            #print(tmp_key)
            cur_ts = request.args['ts']
            #print(cur_ts)
            cur_sign = request.args['sign']
            #print(cur_sign)
            if request.method == 'GET':
                if check_sign(cur_sign, tmp_key, cur_ts):
                    #print('ok')
                    return func(*args, **kwargs)
            if request.method == 'POST':
                tmp_data = request.get_data()
                if check_sign(cur_sign, tmp_key,cur_ts, tmp_data):
                    return func(*args, **kwargs)
            tmp_res = {'status':-1, 'msg': 'crypto error'}
            return json.dumps(tmp_res)
        return dec_func
    return make_dec
'''
def check_request_sign(cur_key):
    def decorate(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            print('check_sign')
            #print(tmp_key)
            cur_ts = request.args['ts']
            #print(cur_ts)
            cur_sign = request.args['sign']
            #print(cur_sign)
            if request.method == 'GET':
                if check_sign(cur_sign, cur_key, cur_ts):
                    #print('ok')
                    return func(*args, **kwargs)
            if request.method == 'POST':
                tmp_data = request.get_data()
                if check_sign(cur_sign, cur_key,cur_ts, tmp_data):
                    return func(*args, **kwargs)
            tmp_res = {'status':-1, 'msg': 'crypto error'}
            return json.dumps(tmp_res)
        return wrapper
    return decorate
    
    
    def make_dec(func):
        def dec_func(*args, **kwargs):
            print('check_sign')
            tmp_key = out_args[0]
            #print(tmp_key)
            cur_ts = request.args['ts']
            #print(cur_ts)
            cur_sign = request.args['sign']
            #print(cur_sign)
            if request.method == 'GET':
                if check_sign(cur_sign, tmp_key, cur_ts):
                    #print('ok')
                    return func(*args, **kwargs)
            if request.method == 'POST':
                tmp_data = request.get_data()
                if check_sign(cur_sign, tmp_key,cur_ts, tmp_data):
                    return func(*args, **kwargs)
            tmp_res = {'status':-1, 'msg': 'crypto error'}
            return json.dumps(tmp_res)
        return dec_func
    return make_dec




#初始化Bloom Filter
def init_bf(config):
    redis_pool = redis.ConnectionPool(host=config.REDIS_HOST, port=config.REDIS_PORT)
    redis_handle = redis.StrictRedis(connection_pool=redis_pool, charset='utf-8')
    cur_bf = bfredis(config.BF_KEY, redis_handle)
    return cur_bf

#验证crawler的url的装饰器
def check_crawler_url(func):
    @wraps(func)
    def dec_func(*args, **kwargs):   
        #print('check_crawler_url')        
        data_item = json.loads(request.get_data())
        if data_item['status'] == 1 and data_item['len'] > 0:
            res_list = []
            for item in data_item['data']:
                if cur_bf.do_filter(item['url']):
                    res_list.append(item)
            
            request.url_list = res_list
            return func(*args, **kwargs)
        else:
            return {'status': -1, 'msg': 'no content'}    
    return dec_func